package org.edu.modules.jxgz.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.jxgz.api.CommonApi;
import org.edu.modules.jxgz.config.ConfigConstant;
import org.edu.modules.jxgz.entity.VeJwGzzj;
import org.edu.modules.jxgz.entity.VeJwText;
import org.edu.modules.jxgz.service.IVeJwGzzjService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.edu.modules.jxgz.util.VeJwTextUtils;
import org.edu.modules.scheduleCommon.entity.VeBaseAppUser;
import org.edu.modules.scheduleCommon.entity.VeBaseTeacher;
import org.edu.modules.scheduleCommon.service.VeBaseAppUserService;
import org.edu.modules.scheduleCommon.service.VeBaseTeacherService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: ve_jw_gzzj
 * @Author: jeecg-boot
 * @Date: 2021-05-26
 * @Version: V1.0
 */
@Api(tags = "工作总结信息表")
@RestController
@RequestMapping("/jxgz/veJwGzzj")
@Slf4j
public class VeJwGzzjController extends BaseController<VeJwGzzj, IVeJwGzzjService> {
    @Autowired
    private IVeJwGzzjService veJwGzzjService;
    @Autowired
    private CommonApi commonApi;

    @Autowired
    private VeBaseAppUserService veBaseAppUserService;

    @Autowired
    private VeBaseTeacherService veBaseTeacherService;

    /**
     * 分页列表查询
     *
     * @param veJwGzzj
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "工作总结-分页列表查询")
    @ApiOperation(value = "工作总结-分页列表查询", notes = "工作总结-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(VeJwGzzj veJwGzzj,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeJwGzzj> queryWrapper = QueryGenerator.initQueryWrapper(veJwGzzj, req.getParameterMap());
        queryWrapper.eq("status", ConfigConstant.STATUS_1);
        Page<VeJwGzzj> page = new Page<VeJwGzzj>(pageNo, pageSize);
        IPage<VeJwGzzj> pageList = veJwGzzjService.page(page, queryWrapper);
        for (VeJwGzzj temp : pageList.getRecords()) {
            VeJwText veJwTextById = VeJwTextUtils.getVeJwTextById(temp.getGzzjtid());
            if (veJwTextById != null) {
                temp.setGzzjtidText(veJwTextById.getText());
            }
        }
        return Result.ok(pageList);
    }


    /**
     * 分页列表查询-我的
     *
     * @param veJwGzzj
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "工作总结-分页列表查询-我的工作总结")
    @ApiOperation(value = "工作总结-分页列表查询-我的", notes = "工作总结-分页列表查询-我的")
    @GetMapping(value = "/mylist")
    public Result<?> queryMyPageList(VeJwGzzj veJwGzzj,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                     HttpServletRequest req) {
        QueryWrapper<VeJwGzzj> queryWrapper = QueryGenerator.initQueryWrapper(veJwGzzj, req.getParameterMap());
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        queryWrapper.eq("addUser", appUser.getUserId());
        Page<VeJwGzzj> page = new Page<VeJwGzzj>(pageNo, pageSize);
        IPage<VeJwGzzj> pageList = veJwGzzjService.page(page, queryWrapper);
        for (VeJwGzzj temp : pageList.getRecords()) {
            VeJwText veJwText = VeJwTextUtils.getVeJwTextById(temp.getGzzjtid());
            if (veJwText != null) {
                temp.setGzzjtidText(veJwText.getText());
            }
            veJwText = VeJwTextUtils.getVeJwTextById(temp.getVerifytid());
            if (veJwText != null) {
                temp.setVerifytidText(veJwText.getText());
            }
        }
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param veJwGzzj
     * @return
     */
    @Transactional
    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJwGzzj veJwGzzj) {
        String rznrtidText = veJwGzzj.getGzzjtidText();
        if (StringUtils.isBlank(rznrtidText)) {
            return Result.error("内容不可为空");
        }
		/*try {
			Result<JSONObject> jsonObjectResult = commonApi.queryAppUserByUserId(CommonApi.COMMON_TOKEN_KEY, veJwGzzj.getAdduser());
			if (jsonObjectResult == null || jsonObjectResult.getResult() != null) {
				JSONObject result = jsonObjectResult.getResult();
				veJwGzzj.setAddusername(result.getString("userName"));
				Result<JSONObject> techerResult = commonApi.queryTeacherByUserId(CommonApi.COMMON_TOKEN_KEY, result.getString("userId"));
				if (techerResult == null || techerResult.getResult() == null) {
					return Result.error("当前登录帐号非教师帐号");
				}
				veJwGzzj.setJsgh(jsonObjectResult.getResult().getString("gh"));
			}else {
				return Result.error("用户信息获取失败");
			}
		} catch (Exception e) {
			return Result.error("教师信息获取失败");
		}*/

        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        veJwGzzj.setAddusername(appUser.getRealName());
        veJwGzzj.setIsAudited(ConfigConstant.AUDITED);
        veJwGzzj.setGzzjtid(VeJwTextUtils.add(rznrtidText));
        veJwGzzjService.save(veJwGzzj);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param veJwGzzj
     * @return
     */
    @AutoLog(value = "ve_jw_gzzj-编辑")
    @ApiOperation(value = "ve_jw_gzzj-编辑", notes = "ve_jw_gzzj-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJwGzzj veJwGzzj) {
        String rznrtidText = veJwGzzj.getGzzjtidText();
        if (StringUtils.isBlank(rznrtidText)) {
            return Result.error("内容不可为空");
        }
        VeJwTextUtils.update(veJwGzzj.getGzzjtid(), rznrtidText);
        veJwGzzjService.updateById(veJwGzzj);
        return Result.ok("编辑成功!");
    }


    /**
     * 评审
     *
     * @param veJwGzzj
     * @return
     */
    @AutoLog(value = "评审")
    @ApiOperation(value = "评审", notes = "评审")
    @PutMapping(value = "/audit")
    public Result<?> audit(@RequestBody VeJwGzzj veJwGzzj) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        veJwGzzj.setVerifyusername(appUser.getRealName());
        if (StringUtils.isNotBlank(veJwGzzj.getVerifytidText())) {
            veJwGzzj.setVerifytid(VeJwTextUtils.addOrUpdate(veJwGzzj.getVerifytid(), veJwGzzj.getVerifytidText()));
        }
        veJwGzzj.setIsAudited(ConfigConstant.AUDITED_YES);
        veJwGzzj.setVerifytime(new Date());
        veJwGzzjService.updateById(veJwGzzj);
        return Result.ok("编辑成功!");
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ve_jw_gzzj-通过id删除")
    @ApiOperation(value = "ve_jw_gzzj-通过id删除", notes = "ve_jw_gzzj-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        veJwGzzjService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ve_jw_gzzj-批量删除")
    @ApiOperation(value = "ve_jw_gzzj-批量删除", notes = "ve_jw_gzzj-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.veJwGzzjService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ve_jw_gzzj-通过id查询")
    @ApiOperation(value = "ve_jw_gzzj-通过id查询", notes = "ve_jw_gzzj-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeJwGzzj veJwGzzj = veJwGzzjService.getById(id);
        if (veJwGzzj == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(veJwGzzj);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param veJwGzzj
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwGzzj veJwGzzj) {
        return super.exportXls(request, veJwGzzj, VeJwGzzj.class, "ve_jw_gzzj");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwGzzj.class);
    }

}
